// PB5 LED
// PC3 DIR
// PC4 STEP

#include <iostm8s003f3.h>
#include <intrinsics.h>

#define NO_OP_CYCLES 80 // 80 for tb6600, 16 for A4988

static void delay(unsigned long nCount)
{
  while (nCount != 0) {
    nCount--;
  }
}

int main( void )
{
  unsigned int i = 1500;
  unsigned int j;
  
  // setup clock
  CLK_CKDIVR &= (unsigned char)(~0x18);  // 16MHz high speed internal clock
  
  // PB5: LED
  PB_DDR |= 0x20;  // output
  PB_CR1 |= 0x20;  // push pull
  PB_CR2 |= 0x20;  // 10MHz output speed
  
  // PC3: DIR;
  // PC4: STEP;
  PC_DDR |= 0x18;  // output
  PC_CR1 |= 0x18;  // push pull
  PC_CR2 |= 0x18;  // 10MHz output speed
  PC_ODR &= 0xE7;  // dir = step = 0


  while (1) {
    PC_ODR |= 0x10;  // Step HIGH
    PB_ODR &= 0xDF;  // LOW is ON
    for (j=0; j<NO_OP_CYCLES; j++)
      __no_operation();
    /*
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    __no_operation();
    */

    PC_ODR &= 0xEF;  // Step LOW
    PB_ODR |= 0x20;  // HIGH if OFF
    
    delay(i);
    if (i>10)  // i should be larger than 10 for safety
      i--;
  } 
}
